package com.huiying.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;

/**
 * @Author: lsh
 * @Date: 2024/10/27/11:25
 * @Description: redis序列化器
 */
@Configuration
public class RedisConfig {

    @Bean
    public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory connectionFactory) {
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(connectionFactory);

        // 使用 StringRedisSerializer 作为 key 的序列化器
        redisTemplate.setKeySerializer(new StringRedisSerializer());

        // 使用 StringRedisSerializer 作为 value 的序列化器，确保存储的内容是字符串类型
        redisTemplate.setValueSerializer(new StringRedisSerializer());

        // 设置 hash 的 key 和 value 的序列化方式
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setHashValueSerializer(new StringRedisSerializer());

        return redisTemplate;
    }
}

